package zjitc.controller;

import com.github.pagehelper.PageInfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import zjitc.po.Book;
import zjitc.po.BookCategory;
import zjitc.service.BookCategoryService;
import zjitc.service.BookService;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/26 15:16
 * @description:
 */
@Controller
@RequestMapping("/category")
public class BookCategoryController {
  @Autowired
  private BookService bookService;
  @Autowired
  private BookCategoryService bookCategoryService;
  @RequestMapping("/index")
      public String index(Model model)
  {
    List<BookCategory> list=bookCategoryService.findAllCategory();
    List<Book> hotList=bookService.findisHotBook();
    List<Book> newList=bookService.findisNewBook();
        model.addAttribute("list",list);
        model.addAttribute("hotList",hotList);
        model.addAttribute("newList",newList);
       return "bookPage/index";
  }
    @RequestMapping("/byCategory")
    public String byCategory(HttpServletRequest request,Model model,@RequestParam(name = "count", required = false, defaultValue = "8") int count)
    {
      int start = Integer.valueOf(request.getParameter("start"));
      String categoryname = request.getParameter("categoryname");
        List<Book> bookList= bookService.findBookByCategoryNameByPage(categoryname,start,count);
        model.addAttribute("categoryList",bookList);
        List<BookCategory> list=bookCategoryService.findAllCategory();
      PageInfo pageInfo=new PageInfo(bookList);
      model.addAttribute("page", pageInfo);
        model.addAttribute("list",list);
        model.addAttribute("categoryname",categoryname);
        return "bookPage/bookList";
    }

  @RequestMapping("/sendToBookMessageCategory")
  public ModelAndView sendToBookMessageCategory(HttpServletRequest request)
  {
    String categoryname = request.getParameter("categoryname");
    ModelAndView modelAndView=new ModelAndView();
    List<BookCategory> list=bookCategoryService.findAllCategory();
    modelAndView.addObject("list",list);
    modelAndView.setView(new InternalResourceView("/WEB-INF/bookPage/bookMessage.jsp"));
    return modelAndView;
  }
}
